package cn.chenxi.hadoopproject.temperature.controller;

import cn.chenxi.hadoopproject.base.response.JsonResult;
import cn.chenxi.hadoopproject.temperature.pojo.dto.TemperatureQuery;
import cn.chenxi.hadoopproject.temperature.pojo.vo.TemperatureVO;
import cn.chenxi.hadoopproject.temperature.service.TemperatureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "平均气温查询")
@RequestMapping("/v1/temperature")
public class TemperatureController {
    @Autowired
    private TemperatureService temperatureService;

    @GetMapping("/select")
    @ApiOperation("查找对应城市对应年份的平均月气温")
    public JsonResult select(TemperatureQuery temperatureQuery) {
        List<TemperatureVO> temperatureVO = temperatureService.select(temperatureQuery);
        return JsonResult.ok(temperatureVO);
    }

    @GetMapping("/desc")
    @ApiOperation("对最高温进行降序排列")
    public JsonResult desc(TemperatureQuery temperatureQuery) {

        // 假设这是你的 TemperatureVO 列表
        List<TemperatureVO> temperatureList = select2(temperatureQuery); // 初始化你的列表

        // 使用Lambda表达式和Comparator进行降序排序
        temperatureList.sort(Comparator.comparing(TemperatureVO::getMaxTemp).reversed());

        // 或者直接在sort方法中使用lambda表达式
        // temperatureList.sort((t1, t2) -> Float.compare(t2.getMaxTemp(), t1.getMaxTemp()));

        // 排序后处理你的列表...
        return JsonResult.ok(temperatureList);
    }

    @GetMapping("/asc")
    @ApiOperation("对最低温进行升序排列")
    public JsonResult asc(TemperatureQuery temperatureQuery) {
        List<TemperatureVO> temperatureList = select2(temperatureQuery); // 初始化你的列表

        // 使用Lambda表达式和Comparator进行升序排序
        temperatureList.sort(Comparator.comparing(TemperatureVO::getMinTemp));

        // 或者直接在sort方法中使用lambda表达式
        // temperatureList.sort((t1, t2) -> Float.compare(t1.getMinTemp(), t2.getMinTemp()));

        // 排序后处理你的列表...
        return JsonResult.ok(temperatureList);
    }

    private List<TemperatureVO> select2(TemperatureQuery temperatureQuery) {
        return temperatureService.select(temperatureQuery);
    }
}
